package com.henghai.zhenxuan.ui.page.mine;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.MyFastMailBean;
import com.henghai.zhenxuan.ui.page.adapter.MyFastMailAdapter;
import com.henghai.zhenxuan.R;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的快递
 */
public class MyFastMailFragment extends BaseFragment {

    private MyFastMailStates mStates;
    private RefreshLayout mRefreshLayout;
    private MyFastMailAdapter adapter;

    private static final String TAG = "MyFastMailFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        List<MyFastMailBean> taskList = new ArrayList<>();
//        taskList.add(new MyFastMailBean("1", "123123123", "待收货", "2025-08-08"));
//        taskList.add(new MyFastMailBean("2", "43234", "已收货", "2025-08-08"));
//        taskList.add(new MyFastMailBean("3", "43554453", "已发出", "2025-08-08"));
//        taskList.add(new MyFastMailBean("4", "565656546", "派送中", "2025-08-08"));
        mStates.taskList.set(taskList);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(MyFastMailStates.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        adapter = new MyFastMailAdapter(getContext());
        adapter.setListener(new MyFastMailAdapter.OnItemBtClickListener() {
            @Override
            public void onItemDelClick(MyFastMailBean bean, int postion) {
                Log.d(TAG, "onItemDelClick: 查看物流");
                Log.d(TAG, "onItemDelClick: " + bean.toString());
            }
        });
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_myfastmail, BR.vm, mStates)
                .addBindingParam(BR.listener, new ListenerRefreshHandler())
                .addBindingParam(BR.loadListener, new ListenerLoadHandler())
                .addBindingParam(BR.click, new ClickProxy())
                .addBindingParam(BR.adapter, adapter);
    }


    public class ClickProxy {
        public void back() {
            Log.d(TAG, "back: ");
            nav().popBackStack();
        }

        public void fastMailDetails() {
            //跳转快递详情页  缺少快递单号 暂时不能点
//            nav().navigate(R.id.action_myFastMailFragment_to_fastMailDetailsFragment);
        }
    }

    public class ListenerRefreshHandler implements OnRefreshListener {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            mStates.pageNum = 1;
            mRefreshLayout = refreshLayout;
        }
    }

    public class ListenerLoadHandler implements OnLoadMoreListener {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            mRefreshLayout = refreshLayout;
            if (mStates.rows >= Integer.parseInt(mStates.PageSize)) {
                mStates.pageNum++;
            } else {
                mRefreshLayout.finishLoadMore();
//                mRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    }

    public static class MyFastMailStates extends StateHolder {
        public int pageNum = 1;
        public int rows;
        public final String PageSize = "20";
        public final State<List<MyFastMailBean>> taskList = new State<>(new ArrayList<>());
    }
}
